<?php
namespace app\controllers;
use app\models\Class2;
use app\models\DataRelationship;
use app\models\Notebook;
use app\models\WritePerson;
use \yii\web\Controller;
use app\models\WriteQuestion;
class WritingController extends Controller
{
    public $layout = 'writing';
    public function beforeAction($action)
    {
        $session = \Yii::$app->session;
        $session->open();
        if (!isset($session['user_id'])) {
            if(isset($_COOKIE['dianke_user_id']))
                $this->redirect('?r=site/dologin&islogin='.$_COOKIE['dianke_user_id']);
            else
                $this->redirect('?r=site/login');
        }
        return parent::beforeAction($action);
    }
    public function actionIndex()
    {
        $session = \Yii::$app->session;
        $session->open();
        $session['write_complete']=false;
        $request = \Yii::$app->request;
        if($request->isAjax)
        {
            $note=$request->post('saved-note');
            $my_note=$session['write_note'];
            $my_note->note=$note;
            $my_note->save();
            $session['write_note']=$my_note;
        }
        elseif($request->isGet)
        {
            $id = $request->get('id');
            if(!isset($id)) die("error");
            $this->layout = 'direction';
            $session = \Yii::$app->session;
            $session->open();
            $action = 'comprehensive';
            $write_question = WriteQuestion::findOne($id);
            $where = [
                'class1_id' => $write_question['class1_id'],
                'class2_id' => $write_question['class2_id'],
                'question_id' => $write_question['id'],
                'user_id' => $session['user_id']
            ];
            $my_note = Notebook::find()->where($where)->one();
            if (!isset($my_note)) {
                $my_note = new Notebook();
                $my_note->class1_id = $write_question['class1_id'];
                $my_note->class2_id = $write_question['class2_id'];
                $my_note->question_id = $write_question['id'];
                $my_note->user_id = $session['user_id'];
                $my_note->save();
            }
            $session['write_question']=$write_question;
            $session['write_note'] = $my_note;
            if($write_question['class_name']=='综合写作')
                return $this->render('direction1', ['action' => $action,'write_question'=>$write_question]);
            else
                return $this->render('direction2', ['action' => $action,'write_question'=>$write_question]);
        }
        elseif($request->isPost)
        {
            $my_note=$session['write_note'];
            $page = $request->post('page');
            $practice = $request->post('waytopra');
            $write_content=$session['write_question'];
            switch($write_content['class_name'])
            {
                case "独立写作":
                    $action="independent";
                    $type=1;
                    break;
                case "综合写作":
                    $action="comprehensive";
                    $type=2;
                    break;
                default:
                    die("writing class error");
            }
            $class2=Class2::findOne($write_content['class2_id']);
            $write_person=WritePerson::find()->where(['uid'=>$session['user_id'],'type'=>$type,'write_question_id'=>$write_content['id']])->one();
            if ($page == 1) {
                $state = 'begin';
                if (isset($practice)) {
                    $session['write_practice'] = true;
                } else {
                    $session['write_practice'] = false;
                }
            } else {
                $state = 'next';
            }
            if (!isset($write_person))
                $write_person = new WritePerson();
            if($page>1)
                switch($session['form'])
                {
                    case 2:
                        $composition = $request->post('composition');
                        $write_person->readtheme = $session["total_views3"];
                        $write_person->readhand1 = $session["first_view3"];
                        $write_person->readhand2 = $session["second_view3"];
                        $write_person->readhand3 = $session["third_view3"];
                        $write_person->listentheme = $session["total_views1"];
                        $write_person->listenhand1 = $session["first_view1"];
                        $write_person->listenhand2 = $session["second_view1"];
                        $write_person->listenhand3 = $session["third_view1"];
                        $write_person->content = $composition;
                        $write_person->type = $type;
                        $write_person->uid = $session['user_id'];
                        $write_person->write_question_id = $write_content['id'];
                        $now = time();
                        $write_person->time = $now - $session['begin_time'];
                        $write_person->save();
                        $session['write_person_id'] = $write_person->attributes['id'];
                        $session['form'] = 0;
                        break;
                    default:
                        $total_views = $request->post('total-views');
                        $first_view = $request->post('first-view');
                        $second_view = $request->post('second-view');
                        $third_view = $request->post('third-view');
                        $session['total_views' . $session['form']] = $total_views;
                        $session['first_view' . $session['form']] = $first_view;
                        $session['second_view' . $session['form']] = $second_view;
                        $session['third_view' . $session['form']] = $third_view;
                        $session['form'] = 0;
                        break;

                }

            $result = [
                'write_content' => $write_content,
                'page' => $page + 1,
                'state' => $state,
                'action' => $action,
                'token' => $session['time_token'],
                'my_note' => $my_note,
                'class2'=>$class2
            ];
            switch ($page)
            {
                case 1:
                    return $this->render('3', $result);
                    break;
                case 2:
                    return $this->render($type==1?'4':'1', $result);
                    break;
                case 3:
                    if($type==1)
                    {
                        $session['write_complete'] = true;
                        $this->saveState($write_content);
                        $this->redirect('?r=community/writing&id='.$write_content['id']);
                    }
                    else
                        return $this->render('2', $result);
                    break;
                case 4:
                    $session['write_complete'] = true;
                    $this->saveState($write_content);
                    $this->redirect('?r=community/writing&id='.$write_content['id']);
                    break;
                default:
                    die("error");
            }
        }

    }

//    public function actionInndex()
//    {
//        $session = \Yii::$app->session;
//        $session->open();
//        $session['complete']=false;
//        $request = \Yii::$app->request;
//        if ($request->get('id')) {
//            $id = $request->get('id');
//            $session['write_content_id'] = $id;
//        }
//        $session['user_id_write_person_type1']=WritePerson::find()->where(['uid'=>$session['user_id'],'type'=>1,'write_question_id'=>$session['write_content_id']])->one();
//        $session['user_id_write_person_type2']=WritePerson::find()->where(['uid'=>$session['user_id'],'type'=>2,'write_question_id'=>$session['write_content_id']])->one();
//        $session['write_userid_id_type1']=$session['user_id_write_person_type1']['id'];
//        $session['write_userid_id_type2']=$session['user_id_write_person_type2']['id'];
//        $write_question_judge = WriteQuestion::find()->where(['id' => $session['write_content_id']])->one();
//        $write_question_class2_judge = Class2::find()->where(['id' => $write_question_judge['class2_id']])->one();
//        if($request->isAjax)
//        {
//            $note=$request->post('saved-note');
//            $my_note=$session['write_note'];
//            $my_note->note=$note;
//            $my_note->save();
//            $session['write_note']=$my_note;
//        }
//        else {
//            $my_note=$session['write_note'];
//            if ($write_question_class2_judge['name'] == '综合写作')
//            {
//                $session = \Yii::$app->session;
//                $session->open();
//                $action = 'comprehensive';
//                $request = \Yii::$app->request;
//                if ($request->isPost)
//                {
//                    $class_2_id = Class2::find()->where(['name' => '综合写作'])->one();
//                    $write_content = WriteQuestion::find()->where(['id' => $session['write_content_id'], 'class2_id' => $class_2_id])->one();
//                    $page = $request->post('page');
//                    $practice = $request->post('waytopra');
//                    if (!isset($session['user_id_write_person_type1']))//write persion
//                    {
//                        if ($session['form'] == 1) {
//                            $total_views1 = $request->post('total-views1');
//                            $first_view1 = $request->post('first-view1');
//                            $second_view1 = $request->post('second-view1');
//                            $third_view1 = $request->post('third-view1');
//                            $session['total_views1'] = $total_views1;
//                            $session['first_view1'] = $first_view1;
//                            $session['second_view1'] = $second_view1;
//                            $session['third_view1'] = $third_view1;
//                            $session['form'] = 0;
//                        } else if ($session['form'] == 3) {
//                            $total_views3 = $request->post('total-views3');
//                            $first_view3 = $request->post('first-view3');
//                            $second_view3 = $request->post('second-view3');
//                            $third_view3 = $request->post('third-view3');
//                            $session['total_views3'] = $total_views3;
//                            $session['first_view3'] = $first_view3;
//                            $session['second_view3'] = $second_view3;
//                            $session['third_view3'] = $third_view3;
//                            $session['form'] = 0;
//                        } else if ($session['form'] == 2) {
//                            $composition = $request->post('composition');
//                            $write_person = new WritePerson();
//                            $write_person->readtheme = $session["total_views3"];
//                            $write_person->readhand1 = $session["first_view3"];
//                            $write_person->readhand2 = $session["second_view3"];
//                            $write_person->readhand3 = $session["third_view3"];
//                            $write_person->listentheme = $session["total_views1"];
//                            $write_person->listenhand1 = $session["first_view1"];
//                            $write_person->listenhand2 = $session["second_view1"];
//                            $write_person->listenhand3 = $session["third_view1"];
//                            $write_person->content = $composition;
//                            $write_person->type = 1;
//                            $write_person->uid = $session['user_id'];
//                            $write_person->write_question_id = $session['write_content_id'];
//                            $now = time();
//                            $write_person->time = $now - $session['begin_time'];
//                            $write_person->save();
//                            $session['write_person_id'] = $write_person->attributes['id'];
//                            $session['form'] = 0;
//                        }
//                    }
//                    elseif (isset($session['user_id_write_person_type1'])) ////write persion
//                    {
//                        if ($session['form'] == 1) {
//                            $total_views1 = $request->post('total-views1');
//                            $first_view1 = $request->post('first-view1');
//                            $second_view1 = $request->post('second-view1');
//                            $third_view1 = $request->post('third-view1');
//                            $session['total_views1'] = $total_views1;
//                            $session['first_view1'] = $first_view1;
//                            $session['second_view1'] = $second_view1;
//                            $session['third_view1'] = $third_view1;
//                            $session['form'] = 0;
//                        } else if ($session['form'] == 3) {
//                            $total_views3 = $request->post('total-views3');
//                            $first_view3 = $request->post('first-view3');
//                            $second_view3 = $request->post('second-view3');
//                            $third_view3 = $request->post('third-view3');
//                            $session['total_views3'] = $total_views3;
//                            $session['first_view3'] = $first_view3;
//                            $session['second_view3'] = $second_view3;
//                            $session['third_view3'] = $third_view3;
//                            $session['form'] = 0;
//                        } else if ($session['form'] == 2) {
//                            $composition = $request->post('composition');
//                            $write_person = WritePerson::findOne($session['write_userid_id_type1']);
//                            $write_person->readtheme = $session["total_views3"];
//                            $write_person->readhand1 = $session["first_view3"];
//                            $write_person->readhand2 = $session["second_view3"];
//                            $write_person->readhand3 = $session["third_view3"];
//                            $write_person->listentheme = $session["total_views1"];
//                            $write_person->listenhand1 = $session["first_view1"];
//                            $write_person->listenhand2 = $session["second_view1"];
//                            $write_person->listenhand3 = $session["third_view1"];
//                            $write_person->content = $composition;
//                            $write_person->type = 1;
//                            $write_person->uid = $session['user_id'];
//                            $write_person->write_question_id = $session['write_content_id'];
//                            $now = time();
//                            $write_person->time = $now - $session['begin_time'];
//                            $data = $write_person->save();
//                            $session['write_person_id'] = $write_person->attributes['id'];
//                            $session['form'] = 0;
//                        }
//                    }
//                    if ($page == 1) {
//                        if (isset($practice)) {
//                            $session['practice'] = 1;
//                        } else {
//                            $session['practice'] = 0;
//                        }
//                    }
//                    if ($page == 1) {
//                        $state = 'begin';
//                    } else {
//                        $state = 'next';
//                    }
//                    $result = [
//                        'write_content' => $write_content,
//                        'page' => $page + 1,
//                        'state' => $state,
//                        'action' => $action,
//                        'token' => $session['time_token'],
//                        'my_note' => $my_note,
//                        'write_question_class2_judge'=>$write_question_class2_judge
//                    ];
//
//                    if ($page == 1) {
//                        return $this->render('3', $result);
//                    }
//                    if ($page == 2) {
//                        return $this->render('1', $result);
//                    }
//                    if ($page == 3) {
//                        return $this->render('2', $result);
//                    }
//                    if ($page == 4) {
//
//                    }
//                } else {
//                    $this->layout = 'direction';
//                    $session = \Yii::$app->session;
//                    $session->open();
//                    $request = \Yii::$app->request;
//                    //todo
//                    $class_2_id = Class2::find()->where(['name' => '综合写作'])->one();
//                    $write_question = WriteQuestion::find()->where(['id' => $session['write_content_id'], 'class2_id' => $class_2_id['id']])->one();
//
//                    $where = [
//                        'class1_id' => $write_question['class1_id'],
//                        'class2_id' => $write_question['class2_id'],
//                        'question_id' => $write_question['id'],
//                        'user_id' => $session['user_id']
//                    ];
//                    $my_note = Notebook::find()->where($where)->one();
//                    if (!isset($my_note)) {
//                        $my_note = new Notebook();
//                        $my_note->class1_id = $write_question['class1_id'];
//                        $my_note->class2_id = $write_question['class2_id'];
//                        $my_note->question_id = $write_question['id'];
//                        $my_note->user_id = $session['user_id'];
//                        $my_note->save();
//                    }
//
//                    $session['write_note'] = $my_note;
//                    $session['class_2_name'] = $class_2_id['name'];
//                    $session['write_question_num'] = count($write_question);
//                    $session['reset'] = 1;
//                    return $this->render('direction', ['action' => $action]);
//                }
//            }
//            elseif ($write_question_class2_judge['name'] == '独立写作')
//            {
//                $session = \Yii::$app->session;
//                $session->open();
//                $action = '';
//                $request = \Yii::$app->request;
//                if ($request->isPost) {
//                    $class_2_id = Class2::find()->where(['name' => '独立写作'])->one();
//                    $write_content = WriteQuestion::find()->where(['id' => $session['write_content_id'], 'class2_id' => $class_2_id])->one();
//                    $page = $request->post('page');
//                    $practice = $request->post('waytopra');
//                    if (!isset($session['user_id_write_person_type2'])) {
//                        if ($session['form'] == 3) {
//                            $total_views3 = $request->post('total-views3');
//                            $first_view3 = $request->post('first-view3');
//                            $second_view3 = $request->post('second-view3');
//                            $third_view3 = $request->post('third-view3');
//                            $session['total_views3'] = $total_views3;
//                            $session['first_view3'] = $first_view3;
//                            $session['second_view3'] = $second_view3;
//                            $session['third_view3'] = $third_view3;
//                            $session['form'] = 0;
//                        } else if ($session['form'] == 2) {
//                            $composition = $request->post('composition');
//                            $write_person2 = new WritePerson();
//                            $write_person2->readtheme = $session["total_views3"];
//                            $write_person2->readhand1 = $session["first_view3"];
//                            $write_person2->readhand2 = $session["second_view3"];
//                            $write_person2->readhand3 = $session["third_view3"];
//                            $write_person2->listentheme = 'null';
//                            $write_person2->listenhand1 = 'null';
//                            $write_person2->listenhand2 = 'null';
//                            $write_person2->listenhand3 = 'null';
//                            $write_person2->content = $composition;
//                            $write_person2->type = 2;
//                            $write_person2->uid = 1;
//                            $write_person2->write_question_id = $session['write_content_id'];
//                            $now = time();
//                            $write_person2->time = $now - $session['begin_time'];
//                            $data = $write_person2->save();
//                            $session['write_person_id'] = $write_person2->attributes['id'];
//                            $session['form'] = 0;
//
//                        }
//                    }
//                    elseif (isset($session['user_id_write_person_type2'])) {
//                        if ($session['form'] == 3) {
//                            $total_views3 = $request->post('total-views3');
//                            $first_view3 = $request->post('first-view3');
//                            $second_view3 = $request->post('second-view3');
//                            $third_view3 = $request->post('third-view3');
//                            $session['total_views3'] = $total_views3;
//                            $session['first_view3'] = $first_view3;
//                            $session['second_view3'] = $second_view3;
//                            $session['third_view3'] = $third_view3;
//                            $session['form'] = 0;
//                        } else if ($session['form'] == 2) {
//                            $composition = $request->post('composition');
//                            $write_person2 = WritePerson::findOne($session['write_userid_id_type2']);
//                            $write_person2->readtheme = $session["total_views3"];
//                            $write_person2->readhand1 = $session["first_view3"];
//                            $write_person2->readhand2 = $session["second_view3"];
//                            $write_person2->readhand3 = $session["third_view3"];
//                            $write_person2->listentheme = 'null';
//                            $write_person2->listenhand1 = 'null';
//                            $write_person2->listenhand2 = 'null';
//                            $write_person2->listenhand3 = 'null';
//                            $write_person2->content = $composition;
//                            $write_person2->type = 2;
//                            $write_person2->uid = 1;
//                            $write_person2->write_question_id = $session['write_content_id'];
//                            $now = time();
//                            $write_person2->time = $now - $session['begin_time'];
//                            $data = $write_person2->save();
//                            $session['write_person_id'] = $write_person2->attributes['id'];
//                            $session['form'] = 0;
//
//                        }
//                    }
//                    if (isset($practice)) {
//                        $session['practice'] = true;
//                    } else {
//                        $session['practice'] = false;
//                    }
//                    if ($page == 1) {
//                        $state = 'begin';
//                    } else {
//                        $state = 'next';
//                    }
//                    $result = [
//                        'write_content' => $write_content,
//                        'page' => $page + 1,
//                        'state' => $state,
//                        'action' => $action,
//                        'token' => $session['time_token'],
//                        'my_note' => $my_note,
//                        'write_question_class2_judge'=>$write_question_class2_judge
//                    ];
//
//                    if ($page == 1) {
//                        return $this->render('3', $result);
//                    }
//                    if ($page == 2) {
//                        return $this->render('4', $result);
//                    }
//                    if ($page == 3) {
//                        $session['complete'] = true;
//                        $this->saveState($write_content);
//                        $this->redirect('?r=community/writing&id=' . $session['write_content_id']);
//                    }
//                } else {
//                    $this->layout = 'direction';
//                    unset($session['write_note']);
//                    $session = \Yii::$app->session;
//                    $session->open();
//                    //todo
//                    $class_2_id = Class2::find()->where(['name' => '独立写作'])->one();
//                    $write_question = WriteQuestion::find()->where(['id' => $session['write_content_id'], 'class2_id' => $class_2_id['id']])->one();
//
//                    $where = [
//                        'class1_id' => $write_question['class1_id'],
//                        'class2_id' => $write_question['class2_id'],
//                        'question_id' => $write_question['id'],
//                        'user_id' => $session['user_id']
//                    ];
//                    $my_note = Notebook::find()->where($where)->one();
//                    if (!isset($my_note)) {
//                        $my_note = new Notebook();
//                        $my_note->class1_id = $write_question['class1_id'];
//                        $my_note->class2_id = $write_question['class2_id'];
//                        $my_note->question_id = $write_question['id'];
//                        $my_note->user_id = $session['user_id'];
//                        $my_note->save();
//                    }
//
//                    $session['write_note'] = $my_note;
//                    $session['class_2_name'] = $class_2_id['name'];
//                    $session['write_question_num'] = count($write_question);
//                    $session['reset'] = 1;
//                    return $this->render('direction', ['action' => $action]);
//                }
//            }
//        }
//    }

    private function saveState($write_question)
    {
        $session=\Yii::$app->session;
        $session->open();
        $where=[
            'user_id'=>$session['user_id'],
            'class1_id'=>$write_question['class1_id'],
            'class2_id'=>$write_question['class2_id'],
            'content_id'=>$write_question['id']
        ];
        $data_relation=DataRelationship::find()->where($where)->one();
        if(!isset($data_relation))
        {
            //新建一条数据
            $data_relation=new DataRelationship();
            $data_relation->user_id=$session['user_id'];
            $data_relation->class1_id=$write_question['class1_id'];
            $data_relation->class2_id=$write_question['class2_id'];
            $data_relation->content_id=$write_question['id'];
        }
        $data_relation->last_time=date('Y-m-d H:i:s');
        $data_relation->save();

        //更新做题人数
        $where=[
            'class1_id'=>$write_question['class1_id'],
            'class2_id'=>$write_question['class2_id'],
            'content_id'=>$write_question['id']
        ];
        $rows=DataRelationship::find()->select(['id'])->where($where)->asArray()->all();
        $write_question->num=count($rows);
        $write_question->save(false);
    }
}